package edu.mtu.citizenscience.cs.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Holds utilities for interacting with images
 */
public class Image {
	
	/**
	 * Loads the passed image view with the image at the passed file
	 * scaling the image to the passed with and height targets
	 * 
	 * @param   imagePath - String - path to the image file
	 * @param	imageView - ImageView - the image view to load with the file
	 * @param	targetW - int - the width bound for the image
	 * @param	targetH - int -  the height bound for the image
	 * @return	void
	 */
	public static void scaleImage(String imagePath, ImageView imageView, int targetW, int targetH)
	{   
		// Options for loading the image through the bitmap factory
	    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
	    // tell the factory to not load the file into memory
	    bmOptions.inJustDecodeBounds = true;
	    // get the image file with the options object
	    BitmapFactory.decodeFile(imagePath, bmOptions);
	    // get the image width and height
	    int photoW = bmOptions.outWidth;
	    int photoH = bmOptions.outHeight;
		
	    // Determine how much to scale down the image
	    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
	  
	    // this time load the image into memory
	    bmOptions.inJustDecodeBounds = false;
	    // set the image scale factor
	    bmOptions.inSampleSize = scaleFactor;
	    // make sure image memory can be overridden
	    bmOptions.inPurgeable = true;
	  
	    // load the image scaled down
	    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
	    // set the image view to hold the image
	    imageView.setImageBitmap(bitmap);
	}
	
}
